package com.banzhi.shop.activity;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.MyPartnerBean;


import com.banzhi.shop.data.MyPartnerData;
import com.banzhi.shop.model.CallBack.MyPartnerCallBack;
import com.banzhi.shop.model.Impl.MyPartnerImpl;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.bumptech.glide.Glide;
import com.github.library.BaseRecyclerAdapter;
import com.github.library.BaseViewHolder;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

//我的合伙人
public class MyPartnersActivity extends BaseActivity implements View.OnClickListener,MyPartnerCallBack {

    private ImageView mIvBackPartner;
    private ImageView mIvSousuoPartner;
    private TextView mTvAllPeople;
    private SwipeRefreshLayout mSrf_partner;
    private RecyclerView mRvPartner;
    private MyPartnerImpl mMyPartnerModel;

    private int page = 1;
    private LinearLayoutManager mLinearLayoutManager;
    private MyPartnerBean mMyPartnerBean;
    List<MyPartnerData.DataBean.CitypartnerBean> CityPartners = new ArrayList<>();
   // private String mCitypartnerCount;
    private BaseRecyclerAdapter<MyPartnerData.DataBean.CitypartnerBean> mRecyclerAdapter;
    private MyPartnerData mMyPartnerData;
    private String mSum2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_partners);
        Translucent.setTranslucent(this);
        initView();
        initListener();
        mMyPartnerModel = new MyPartnerImpl();
        LoadingDialogUtils.showProgress( this, "正在加载,请稍后~" );
        mMyPartnerModel.getMyPartnerData(page+"",this);

        Intent intent = getIntent();
        mSum2 = intent.getStringExtra("sum2");

    }

    private void initView() {
        mIvBackPartner = (ImageView) findViewById(R.id.iv_back_partner);
        mIvSousuoPartner = (ImageView) findViewById(R.id.iv_sousuo_partner);
        mTvAllPeople = (TextView) findViewById(R.id.tv_all_people);
        mSrf_partner = (SwipeRefreshLayout) findViewById(R.id.srf_partner);
        mRvPartner = (RecyclerView) findViewById(R.id.rv_partner);
        mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);

    }


    private void initListener() {
        mIvBackPartner.setOnClickListener(this);
        mIvSousuoPartner.setOnClickListener(this);
        mSrf_partner.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                mMyPartnerModel.getMyPartnerData(page+"",MyPartnersActivity.this);
                mRecyclerAdapter.notifyDataSetChanged();
                mSrf_partner.setRefreshing(false);      //刷新图片消失

            }
        });
        mRvPartner.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLinearLayoutManager) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress( MyPartnersActivity.this, "正在加载,请稍后~" );
                mMyPartnerModel.getMyPartnerData(page+"",MyPartnersActivity.this);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back_partner:
                finish();
                break;
            case R.id.iv_sousuo_partner:
                Intent intent = new Intent(this,FindPartnerActivity.class);

                startActivity(intent);
                break;

        }
    }
    //成功获取我的合伙人数据
    @Override
    public void ongetMyPartnerDataSuccess(Object object) {
        if (mSrf_partner.isRefreshing()) {
            mSrf_partner.setRefreshing(false);
        }
        mMyPartnerData = (MyPartnerData) object;
        List<MyPartnerData.DataBean.CitypartnerBean> citypartner = mMyPartnerData.getData().getCitypartner();

        LogUtils.d(" citypartner ", citypartner.size()+"" );
        if (page == 1) {
            CityPartners.clear();
        } else {
            if ( citypartner.size() == 0){
                return;
            }
        }

        CityPartners.addAll(citypartner);
        LogUtils.d("CityPartners.size():",CityPartners.size()+"");
        showView();
    }
    private void showView() {
        mTvAllPeople.setText(mSum2);


            mRecyclerAdapter = new BaseRecyclerAdapter<MyPartnerData.DataBean.CitypartnerBean>(this, CityPartners, R.layout.recyclerview_item_partners) {
                @Override
                protected void convert(BaseViewHolder baseViewHolder, MyPartnerData.DataBean.CitypartnerBean bean) {
                    baseViewHolder.setText(R.id.tv_nickname, bean.getNickname());
                    baseViewHolder.setText(R.id.tv_time, bean.getCitypartnertime());
                    baseViewHolder.setText(R.id.tv_mobile,bean.getMobile());
                    Glide.with(mContext).load(bean.getAvatar()).placeholder(R.mipmap.zhanweitu2).into((ImageView) baseViewHolder.getView(R.id.civ));

                }
            };
            mRvPartner.setLayoutManager(mLinearLayoutManager);
            mRvPartner.setAdapter(mRecyclerAdapter);
            mRecyclerAdapter.openLoadAnimation(false); //false 不使用动画；

        }

    //获取我的合伙人失败
    @Override
    public void ongetMyPartnerDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }
}
